package controller

import (
	"fmt"
	"gin/response"
	"gin/service"
	"gin/validate"
	"github.com/gin-gonic/gin"
	teamones_helper "github.com/teamones-open/go-helper"
)

// 获取DCC分配配置
func getDCCCategoryDict() (DCCCategoryDict map[string]string) {
	DCCCategoryDict = map[string]string{
		"maya":              "Maya",
		"photoshop":         "Photoshop",
		"ue":                "Unreal Engine",
		"motion_builder":    "MotionBuilder",
		"nuke":              "Nuke",
		"houdini":           "Houdini",
		"substance_painter": "Substance Painter",
	}

	return
}

// 获取工具分类
func getCategoryDict() (CategoryDict map[string]string) {
	CategoryDict = map[string]string{
		"initial_settings":    "初始设置",
		"design":              "概念",
		"story_board":         "故事板",
		"layout":              "Layout",
		"modeling":            "模型",
		"shader_textures":     "材质纹理",
		"camera":              "相机",
		"set_dressing":        "布景",
		"light":               "灯光",
		"rigging":             "绑定",
		"animation":           "动画",
		"cfx":                 "角色特效",
		"vfx":                 "特效",
		"final_setting":       "最终设置与优化",
		"render_output":       "渲染输出",
		"cut":                 "剪辑",
		"compositing":         "合成",
		"auxiliary_functions": "辅助功能",
		"other":               "其它",
	}
	return
}

// 获取工具栏分类列表数据
func GetToolsCategoryList(ctx *gin.Context) {
	DCCCategoryDict := getDCCCategoryDict()
	CategoryDict := getCategoryDict()
	response.Success(ctx, gin.H{"dcc_list": DCCCategoryDict, "category_list": CategoryDict}, "Get category list successfully.")
}

// 添加工具
func AddTool(ctx *gin.Context) {
	var addToolData validate.AddToolData

	err := ctx.ShouldBindJSON(&addToolData)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	DCCCategoryDict := getDCCCategoryDict()
	CategoryDict := getCategoryDict()

	// 判断dcc类型是不是在字典列表范围内
	if _, ok := DCCCategoryDict[addToolData.DCC]; !ok {
		//不存在
		response.Fail(ctx, nil, "DCC type not supported")
		return
	}

	// 判断分类是不是在字典列表范围内
	if _, ok := CategoryDict[addToolData.Category]; !ok {
		//不存在
		response.Fail(ctx, nil, "Category type not supported")
		return
	}

	existTool := service.FindToolsByFileName(addToolData.FileName)

	if existTool.Id > 0 {
		// 存在该应用
		response.Fail(ctx, nil, fmt.Sprintf("tools file name %s already exist.", addToolData.FileName))
		return
	}

	resData, err := service.CreateTools(addToolData)
	if err != nil {
		// 插入失败
		response.Fail(ctx, nil, err.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

// 更新工具
func UpdateTool(ctx *gin.Context) {
	var updateToolData validate.UpdateToolData

	err := ctx.ShouldBindJSON(&updateToolData)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	if updateToolData.FileName != "" {
		//  更新fileName判断filename是否重复
		existTool := service.FindToolsByFileName(updateToolData.FileName)
		if existTool.Id > 0 && uint64(existTool.Id) != updateToolData.Id {
			// 存在该应用
			response.Fail(ctx, nil, fmt.Sprintf("tools file name %s already exist.", updateToolData.FileName))
			return
		}
	}

	resData, err := service.UpdateTool(updateToolData)
	if err != nil {
		// 插入失败
		response.Fail(ctx, nil, err.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

// 删除工具，同时删除工具绑定的DCC数据
func RemoveTool(ctx *gin.Context) {
	var removeToolParam validate.RemoveToolParam

	err := ctx.ShouldBindJSON(&removeToolParam)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	err = service.RemoveToolsById(removeToolParam.ID)
	if err != nil {
		// 删除失败
		response.Fail(ctx, nil, err.Error())
		return
	}

	response.Success(ctx, gin.H{}, "success")
}

// 查询多条工具
func ToolsSelect(ctx *gin.Context) {
	var selectToolQuery validate.SelectToolQuery

	err := ctx.ShouldBindJSON(&selectToolQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := teamones_helper.GenerateSelectQueryParam(selectToolQuery.Param, "tools")

	total, resData, queryErr := service.SelectTools(selectQueryParam)

	DCCCategoryDict := getDCCCategoryDict()
	CategoryDict := getCategoryDict()

	for index, item := range resData {
		resData[index].CategoryText = CategoryDict[item.Category]
		resData[index].DCCText = DCCCategoryDict[item.DCC]
	}

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": gin.H{"total": total, "rows": resData}}, "success")
	}
}

// 把指定工具添加到DCC指定位置
func AddToolsToDCC(ctx *gin.Context) {
	var addDCCToolData validate.AddDCCToolData

	err := ctx.ShouldBindJSON(&addDCCToolData)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	positionDict := map[string]string{
		"menu":     "菜单",
		"tool_box": "工具架",
	}

	if _, ok := positionDict[addDCCToolData.Position]; !ok {
		//不存在
		response.Fail(ctx, nil, "The location field can only use menu and tool_box.")
		return
	}

	// 判断当前关联的工具id 是否是合法的
	existTool := service.FindToolsByID(addDCCToolData.ToolsId)

	if !(existTool.Id > 0) {
		// 不存在该应用
		response.Fail(ctx, nil, fmt.Sprintf("tools id %d not exist.", addDCCToolData.ToolsId))
		return
	}

	resData, err := service.CreateDCCTools(addDCCToolData)
	if err != nil {
		// 插入失败
		response.Fail(ctx, nil, err.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

// 把工具从DCC移除
func RemoveToolsFromDCC(ctx *gin.Context) {

	var removeDCCToolParam validate.RemoveDCCToolParam

	err := ctx.ShouldBindJSON(&removeDCCToolParam)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	err = service.RemoveToolsFromDCCById(removeDCCToolParam.ID)
	if err != nil {
		// 删除失败
		response.Fail(ctx, nil, err.Error())
		return
	}

	response.Success(ctx, gin.H{}, "success")
}

// 查询指定DCC工具列表
func GetDCCToolsList(ctx *gin.Context) {
	var getDCCToolsListParam validate.GetDCCToolsListParam

	err := ctx.ShouldBindJSON(&getDCCToolsListParam)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, queryErr := service.GetDCCToolsList(getDCCToolsListParam)

	DCCCategoryDict := getDCCCategoryDict()
	CategoryDict := getCategoryDict()

	for index, item := range resData {
		resData[index].CategoryText = CategoryDict[item.Category]
		resData[index].DCCText = DCCCategoryDict[item.DCC]
	}

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}
